package com.investment.util.BaseDao;

/**
 * Created by veicth on 17/2/25.
 */
import com.investment.util.DaoException.DbException;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;

public abstract class BaseWriteDao<T> extends BaseReadDao<T> {
    @Autowired
    @Qualifier("masterSqlSessionTemplate")
    private SqlSessionTemplate masterSqlSessionTemplate;


    public BaseWriteDao() {
    }

    private SqlSession getLocalSqlSession(SqlSessionTemplate paramSqlSessionTemplate, boolean paramBoolean) throws DbException {
        return (SqlSession)(paramSqlSessionTemplate == null?this.masterSqlSessionTemplate:paramSqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, paramBoolean));
    }

    public int update() throws DbException {
        return this.update((Object)null);
    }

    public int update(Object bean) throws DbException {
        return this.update("update", bean);
    }

    public int update(String sqlId, Object bean) throws DbException {
        return this.update((SqlSessionTemplate)null, sqlId, bean);
    }

    public int update(SqlSessionTemplate sessionTemplate, String sqlId, Object bean) throws DbException {
        try {
            SqlSession ex = this.getLocalSqlSession((SqlSessionTemplate)null, true);
            return bean == null?ex.update(this.formatSqlId(sqlId)):ex.update(this.formatSqlId(sqlId), bean);
        } catch (Exception var5) {
            throw new DbException(var5);
        }
    }

    public int insert() throws DbException {
        return this.insert((Object)null);
    }

    public int insert(Object bean) throws DbException {
        return this.insert("insert", bean);
    }

    public int insert(String sqlId, Object bean) throws DbException {
        return this.insert((SqlSessionTemplate)null, sqlId, bean);
    }

    public int insert(SqlSessionTemplate sessionTemplate, String sqlId, Object bean) throws DbException {
        try {
            SqlSession ex = this.getLocalSqlSession(sessionTemplate, true);
            return bean != null?ex.insert(this.formatSqlId(sqlId), bean):ex.insert(this.formatSqlId(sqlId));
        } catch (Exception var5) {
            throw new DbException(var5);
        }
    }

    public int delete() throws DbException {
        return this.delete((Object)null);
    }

    public int delete(Object bean) throws DbException {
        return this.delete("delete", bean);
    }

    public int delete(String sqlId, Object bean) throws DbException {
        return this.delete((SqlSessionTemplate)null, sqlId, bean);
    }

    public int delete(SqlSessionTemplate sessionTemplate, String sqlId, Object bean) throws DbException {
        try {
            SqlSession ex = this.getLocalSqlSession(sessionTemplate, true);
            return bean == null?ex.delete(this.formatSqlId(sqlId)):ex.delete(this.formatSqlId(sqlId), bean);
        } catch (Exception var5) {
            throw new DbException(var5);
        }
    }

    public int updateSQL(String sql) throws Exception {
        return this.updateSQL(sql, (Object[])null);
    }

    public int updateSQL(String sql, Object[] beans) throws Exception {
        if(beans == null) {
            beans = new Object[0];
        }

        return this.jdbcTemplate.update(sql, beans);
    }
}

